/*
	Copyright (C) 2013 Pontificia Universidad Catolica del Peru
	contact: http://avatar.inf.pucp.edu.pe/realidad-aumentada/
	address contact: Av. Universitaria 1801, San Miguel, Lima-Peru.
	
	This file is part of UbicAR PUCP.

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with This program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Authors: Alvaro Roger Agüero Sánchez, Rodolfo Javier Galvez Meza
	Pontificia Universidad Catolica del Peru
	Departamento de Ingeniería
*/

package org.grupoavatar.guiaubicua.auxiliares;

import org.grupoavatar.guiaubicua.R;
import org.grupoavatar.guiaubicua.VServicios;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ListaServicioAdapter extends ArrayAdapter<ListaServicioRow> {

	Context context;
	int layoutResourceId;
	ListaServicioRow data[] = null;

	public ListaServicioAdapter(Context context, int layoutResourceId,
			ListaServicioRow[] data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ListaCategoriaHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ListaCategoriaHolder();
			holder.imgIcon = (ImageView) row
					.findViewById(R.id.imgIconListFiltro);
			holder.txtTitle = (TextView) row
					.findViewById(R.id.txtTitleListFiltro);
			holder.cbxSel = (CheckBox) row.findViewById(R.id.cbxListFiltro);

			row.setTag(holder);
		} else {
			holder = (ListaCategoriaHolder) row.getTag();
		}

		holder.cbxSel
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if ((isChecked)
								&& (((VServicios) context).getServiceChecked()
										.get(position) == false)) {
							((VServicios) context).getServiceChecked().set(
									position, true);
							((VServicios) context).clickOnService(position);
						} else if ((!isChecked)
								&& (((VServicios) context).getServiceChecked()
										.get(position) == true)) {
							((VServicios) context).getServiceChecked().set(
									position, false);
							((VServicios) context).clickOnService(position);
						}
					}
				});

		holder.cbxSel.setChecked(((VServicios) context).getServiceChecked()
				.get(position));

		ListaServicioRow cat = data[position];
		holder.txtTitle.setText(cat.title);
		holder.imgIcon.setImageResource(cat.icon);

		return row;
	}

	static class ListaCategoriaHolder {
		ImageView imgIcon;
		TextView txtTitle;
		CheckBox cbxSel;
	}
}